package com.wchen.gdoc.generate.resource.loader;

import com.wchen.gdoc.generate.config.ApplicationConfig;
import com.wchen.gdoc.generate.config.ApplicationConfigProvide;
import java.io.File;
import java.io.FileFilter;
import java.util.*;


/**
 * @Date: 2019/7/2 22:56
 * @Auther: wchen
 * @Version: 1.0.0
 * @Description:
 */
@Deprecated
public class ClassScanningProvider {

    private ClassLoader classLoader;
    public ClassScanningProvider(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    /**
     * 从包package中获取所有的Class
     * @param packageName
     * @return
     */
    public static Set<Class<?>> doScanning(String baseClassPath, String packageName){
        Set<Class<?>> classes = new HashSet<>();
        try {
            String newPackageName = packageName.replace('.', '/');
            if(packageName.endsWith(".class")){
                newPackageName = packageName.substring(0, packageName.length() - 6).replace('.', '/') + ".class";
            }
            String classpath = baseClassPath + newPackageName;
            loadClass(packageName, classpath, classes);
        }catch (Exception e){
            e.printStackTrace();
        }
        return classes;
    }


    /**
     * 以文件的形式来获取包下的所有Class
     * @param packageName
     * @param packagePath
     * @param classes
     */
    private static void loadClass(String packageName, String packagePath, Set<Class<?>> classes){
        File dir = new File(packagePath);
        if(!dir.exists() && !packagePath.endsWith(".class")){
            return ;
        }
        ApplicationConfig applicationConfig = ApplicationConfigProvide.getApplicationConfig();
        ClassLoader defaultClassLoader = applicationConfig.getDefaultClassLoader();
        //一个class文件
        if(!dir.isDirectory() ){
            try {
                classes.add(defaultClassLoader.loadClass(packageName.substring(0, packageName.length() - 6)));
                return;
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }
        }
        //如果存在 就获取包下的所有文件 包括目录
        File[] dirFiles = dir.listFiles(new FileFilter() {
            //自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)
            @Override
            public boolean accept(File file) {
                return file.isDirectory() || (file.getName().endsWith(".class"));
            }
        });

        for (File file : dirFiles) {
            //如果是目录 则继续扫描
            if (file.isDirectory()) {
                String classFilePath = packageName + "." + file.getName();
                loadClass(classFilePath, file.getAbsolutePath(), classes);
            } else {
                //如果是java类文件 去掉后面的.class 只留下类名
                String className = file.getName().substring(0, file.getName().length() - 6);
                try {
                    //添加到集合中去
                    classes.add(defaultClassLoader.loadClass(packageName + '.' + className));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
